package m4_routting;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
            ConnectionFactory f = new ConnectionFactory();
            f.setHost("192.168.64.140");
            f.setPort(5672);
            f.setUsername("admin");
            f.setPassword("admin");
            Channel c = f.newConnection().createChannel();

            //定义 fanout 类型的交换机: logs
            c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
            //向交换机发送消息
            while(true){
                System.out.println("输入消息");
                String msg = new Scanner(System.in).nextLine();
                System.out.println("输入路由键");
                String key = new Scanner(System.in).nextLine();
                //第二个参数指定路由键关键词
                c.basicPublish("logs",key, null,msg.getBytes());
            }
    }
}
